# Build recipe for bzip2.
#
# Copyright (c) 2016-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=bzip2
version=1.0.6
release=2

# Define a category for the output of the package name
pkgcategory=compressors

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch="
https://mirror.fsf.org/dragora/v3/sources/$tarname
https://www.bzip.org/${version}/$tarname
"

description="
A high-quality data compressor program.

Bzip2 is a high quality compressor that uses the best compression
techniques (from the PPM family of statistical compressors).
In general, the compression is considerabily better than other
compressors, based on LZ77/LZ78 algorithm.

The author of bzip2 is Julian Seward.
"

homepage=https://www.bzip.org
license=Custom

# Source documentation
docs="CHANGES LICENSE README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Apply a patch to install the documentation (thanks to LFS)
    patch -Np1 -i "${worktree}/patches/bzip2/bzip2-1.0.6-install_docs-1.patch"

    # Ensure installation of relative symbolic links (thanks to LFS)
    sed -i 's@\(ln -s -f \)$(PREFIX)/bin/@\1@' Makefile

    # Ensure the man pages are installed into the correct location
    sed -i "s@(PREFIX)/man@(PREFIX)/share/man@g" Makefile

    # Ensure location for libraries
    sed -i "s@(PREFIX)/lib@(PREFIX)/lib${libSuffix}@g" Makefile

    # Build bzip2.
    #
    # Provide shared libraries for third party programs
    make -f Makefile-libbz2_so
    make clean

    # Recompile program for static linking
    make LDFLAGS="$QILDFLAGS -static"
    make PREFIX="${destdir}/usr" install

    # Copy shared libraries into package destination
    cp -f -p libbz2.so* "${destdir}/usr/lib${libSuffix}"

    # Library measurement
    strip -s "${destdir}/usr/lib${libSuffix}/libbz2.so.1.0.6"

    # Make soft links providing compatibility
    (
        cd "${destdir}/usr/lib${libSuffix}" || exit 1
        ln -sf libbz2.so.1.0.6 libbz2.so.1.0
        ln -sf libbz2.so.1.0.6 libbz2.so
    )

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy the rest of the documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        cp -p $file "${destdir}/$docsdir"
    done
}

